package com.ahead.customvideo;

import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.view.Surface;

import java.io.IOException;

/**
 * 视频解码的工具类
 */
public class VideoDecoder {
    private MediaExtractor mExtractor;
    private MediaCodec mDecoder;
    /**
     * init MediaExtractor , MediaCodec
     *
     */
    public void init(String filePath, Surface surface){
        try {
            mExtractor = new MediaExtractor();
            mExtractor.setDataSource(filePath);
            int trackCount = mExtractor.getTrackCount();
            for (int i = 0; i <trackCount ; i++) {
                MediaFormat trackFormat = mExtractor.getTrackFormat(i);
                String  mime = trackFormat.getString(MediaFormat.KEY_MIME);
                if(mime.startsWith("video/")){
                    mExtractor.selectTrack(i);
                    //todo 初始化解码器
                    mDecoder = MediaCodec.createDecoderByType(mime); //已初始化状态
                    mDecoder.configure(trackFormat,surface,null,0);                                                 //进入config
                    mDecoder.start();//进入执行状态。
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
